package com.example.myapplication;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Fragment3 extends Fragment {

    private RecyclerView recyclerView;
    private List<Map<String, Object>> discoverList;

    @SuppressLint("MissingInflatedId")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout3, container, false);

        recyclerView = view.findViewById(R.id.recycleview3);

        // 初始化数据
        int[] iconIds = {R.drawable.icon_momets, R.drawable.icon_video, R.drawable.icon_scan, R.drawable.icon_listen, R.drawable.icon_nearby};
        String[] titles = {"朋友圈", "视频号", "扫一扫", "听一听", "附近"};

        discoverList = new ArrayList<>();
        for (int i = 0; i < iconIds.length; i++) {
            Map<String, Object> item = new HashMap<>();
            item.put("icon", iconIds[i]);
            item.put("title", titles[i]);
            discoverList.add(item);
        }

        // 设置适配器
        DiscoverAdapter adapter = new DiscoverAdapter(discoverList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);

        // 点击事件，打开详情页
        adapter.setOnItemClickListener(position -> {
            Intent intent = new Intent(getContext(), DiscoverActivity.class); // 将 DiscoverAdapter.class 改为 DiscoverActivity.class
            intent.putExtra("title", titles[position]);
            startActivity(intent);
        });

        return view;
    }
}
